package mediator;

/**
 * @ClassName MediatorPatternDemo
 * @Description 中介者模式
 * @Author leejiliang
 * @DateTime 2021/2/24 18:13
 * @Version 1.0
 */
public class MediatorPatternDemo {

	public static void main(String[] args) {
		final LandladyA landladyA = new LandladyA();
		final LandladyB landladyB = new LandladyB();
		final LandladyC landladyC = new LandladyC();
		final ConcreteMediator concreteMediator = new ConcreteMediator(landladyA, landladyB, landladyC);
		Buyer buyer = new Buyer(concreteMediator);
		buyer.buyHouse();
	}

	static class Buyer{
		private Mediator mediator;

		public Buyer(Mediator mediator) {
			this.mediator = mediator;
		}
		void buyHouse() {
			mediator.buyBC();
		}
	}


	static class LandladyA{
		void introduce() {
			System.out.println("I am A, I have house");
		}
	}

	static class LandladyB{
		void introduce() {
			System.out.println("I am B, I have house");
		}
	}

	static class LandladyC{
		void introduce() {
			System.out.println("I am C, I have house");
		}
	}

	interface Mediator{
		void buyAB();

		void buyAC();

		void buyBC();
	}

	static class ConcreteMediator implements Mediator{

		private LandladyA landladyA;
		private LandladyB landladyB;
		private LandladyC landladyC;

		public ConcreteMediator(LandladyA landladyA, LandladyB landladyB, LandladyC landladyC) {
			this.landladyA = landladyA;
			this.landladyB = landladyB;
			this.landladyC = landladyC;
		}

		@Override
		public void buyAB() {
			landladyA.introduce();
			landladyB.introduce();
		}

		@Override
		public void buyAC() {
			landladyA.introduce();
			landladyC.introduce();
		}

		@Override
		public void buyBC() {
			landladyB.introduce();
			landladyC.introduce();
		}
	}
}
